// pages/goodsList/goodsList.js
import { getGoodsListByType } from '../../network/index.js';
const { globalData } = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url_pre: globalData.BASE_URL,
        goodsList: {},
        total: 0,
        page: 1,
        pageSize: 10,
        isOK: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({goods_id:options.id})
        this.init();
    },
    init() {
        this.setData({ goodsList:[],page: 1,total:0, isOK: false });
        this.getGoodsListByType();
    },

    async getGoodsListByType() {
        wx.showLoading({
            title: '加载中',
        })
        const {page,pageSize,goods_id:id} = this.data;
        await getGoodsListByType({ page, pageSize, id }).then((result) => {
            const { items, total } = result;
            const data = items.map(item => {
                item.url = this.data.url_pre + item.urls?.split(",")[0] || "default.png";
                item.labels = item.label ? item.label?.split(",") : [];
                return item;
            })
            const goodsList = [...this.data.goodsList, ...data];
            this.setData({
                total,
                page: this.data.page++,
                goodsList
            })
        })
        if (this.data.total <= this.data.goodsList.length) {
            this.setData({ isOK: true });
        }
        wx.hideLoading();
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.init();
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.isOK) {
            return;
        }
        this.getGoodsList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})